var requireData = require('../../../../data/select.js'),
    classData = require('../../../../data/class.js'),
    urlData = require('../../../../data/url.js');
Page({
  data: {
    dateArray: requireData.requireList.dateArray,
    roundArray: requireData.requireList.roundArray,
    index0: 0,//学期
    index1: 0,//轮次
    index2: 0,//button的type
    index3: 1,//button的disabled
    selected: requireData.requireList.selected,
    selected1: requireData.requireList.selected1,
    classArray:classData.classList,
    baomingArray:classData.baomingList,
    typeArray:classData.typeList,
    disabledArray:classData.disabledList,
    type:[],
    disabled:[],
    type1:[],
    disabled1:[],
    userId:0,
    userName:0,
    userGrade:0,
    week:['一','二','三','四','五','六','日'],
    /*url*/
    onloadUrl: urlData.urlList.organManage.selectClass.onload.url,
    bindPickerChangeUrl: urlData.urlList.organManage.selectClass.bindPickerChange.url,
    bindPickerChange1Url: urlData.urlList.organManage.selectClass.bindPickerChange1.url,
    selectedUrl: urlData.urlList.organManage.selectClass.selected.url,
    selected1Url: urlData.urlList.organManage.selectClass.selected1.url,
    okUrl: urlData.urlList.organManage.selectClass.ok.url,
    delUrl: urlData.urlList.organManage.selectClass.del.url
  },
  /*
    加载全部课程
  */
  onLoad:function(e){
    var that = this;
    wx.getStorage({
      key : 'loginConf',
      success : function(res){
        that.setData({
          userId : res.data.userId,
          userName : res.data.userName
        });
        that.requestList(e);
      }
    });
    
  },
  /*
    发送请求当前课程列表
  */
  requestList:function(e){
    var that = this;
    wx.request({
      url: this.data.onloadUrl,  
      method:"get", 
      data: {
        parent_id:that.data.userId,
        course_term:that.data.index0+1,
        course_time:that.data.index1+1,
        click_course:1
      }, 
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },  
      success:function(res) {
        if (res.data.code == 0){
          that.listData(e,res);
        }
      },  
      fail:function(res){  
          console.log('刷list数据失败');  
      }
    });
  },
  /*
    刷list数据
  */
  listData:function(e,res){
    var a = Array.apply(null, Array(100)).map(function(item, i) {
        return 'warn';
    });
    var b = Array.apply(null, Array(100)).map(function(item, i) {
        return false;
    });
    this.setData({
      classArray:res.data.list,
      type : a,
      disabled : b
    });
  },
  /*
    滚动选择--学期
  */
  bindPickerChange: function(e) {
    var 
      selected = this.data.selected,
      flag,
      that = this;
    if(selected){
      flag=1;
    }else{
      flag=0;
    }
    this.setData({
      index0: e.detail.value
    })
    wx.request({
      url: this.data.bindPickerChangeUrl, 
      method:"get",
      data: {
        parent_id:1,
        course_term:parseInt(this.data.index0)+1,
        course_time:parseInt(this.data.index1)+1,
        click_course:flag
      }, 
      header: {'content-type': 'application/x-www-form-urlencoded'},  
      success:function(res) {
        if (res.data.code == 0){
          if (res.data.code == 0){
            if(flag == 1){
              that.setData({
                classArray : res.data.list
              }); 
            }else{
              that.setData({
                baomingArray : res.data.list
              });
            }  
          }
        }
      },  
      fail:function(res){  
          console.log('发送学期失败');  
      }
    });
  },
  /*
    滚动选择--轮次
  */
  bindPickerChange1: function(e) {
    var selected1 = this.data.selected1,
      that = this,
      flag;
    if(selected1){
      flag=0;
    }else{
      flag=1;
    }
    this.setData({
      index1: e.detail.value
    })
    wx.request({
      url: this.data.bindPickerChange1Url, 
      method:"get",
      data: {
        parent_id:1,
        course_term:parseInt(this.data.index0)+1,
        course_time:parseInt(this.data.index1)+1,
        click_course:flag
      }, 
      header: {'content-type': 'application/x-www-form-urlencoded'},  
      success:function(res) {
        if (res.data.code == 0){
          if(flag == 1){
            that.setData({
              classArray : res.data.list
            }); 
          }else{
            that.setData({
              baomingArray : res.data.list
            });
          }  
        }
      },  
      fail:function(res){

      }
    });
  },
  /*
    tab切换(左侧)
  */
  selected: function (e) {
    var that = this;
    wx.request({
      url: this.data.selectedUrl,  
      method:"get",
      data: {
        parent_id:this.data.userId,
        course_term:that.data.index0+1,
        course_time:that.data.index1+1,
        click_course:1
      }, 
      header: {'content-type': 'application/x-www-form-urlencoded'},  
      success:function(res) {
        if (res.data.code == 0){
          that.listData(e,res);
        }
      },  
      fail:function(res){  
          console.log('发送全部课程失败');  
      }
    });
    this.setData({
      selected1: false,
      selected: true
    })
  },
  /*
    tab切换(右侧)
  */
  selected1: function (e) {
    var that = this;

    /*设置退课按钮颜色用的*/
    var a = Array.apply(null, Array(100)).map(function(item, i) {
        return 'primary';
    });
    var b = Array.apply(null, Array(100)).map(function(item, i) {
        return false;
    });
    /*--------------------*/

    wx.request({
      url: this.data.selected1Url,  
      method:"get",
      data: {
        parent_id:this.data.userId,
        course_term:that.data.index0+1,
        course_time:that.data.index1+1,
        click_course:0
      }, 
      header: {'content-type': 'application/x-www-form-urlencoded'},  
      success:function(res) {
        if (res.data.code == 0){
          console.log('发送已报名课程成功');
          console.log(res.data);
          that.setData({
            baomingArray:res.data.list
          });
        }
      },  
      fail:function(res){  
          console.log('发送已报名课程失败');  
      }
    });
    this.setData({
      selected: false,
      selected1: true,
      type1 : a,
      disabled1 : b
    })
  },
  /*
    选课
  */
  selectClass:function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定选择此门课程？',
      success: function(res) {
        if(res.confirm){
          that.ok(e);
        }else if(res.cancel){
          console.log('用户点击取消');
        }
      }
    }) 
  },
  /*
    用户点击了确定按钮，add课程
  */
  ok:function(e){
    var that = this,
      index = e.currentTarget.id;
    wx.request({
      url: this.data.okUrl,   
      method:"get",
      data: {
        parent_id:that.data.userId,
        course_id:that.data.classArray[index].id
      },
      header: {'content-type': 'application/x-www-form-urlencoded'},  
      success:function(res) {
        if (res.data.code == 0){
          that.setButton(e);
        }else{
          wx.showModal({
            title: '提示',
            content: '报名人数已满！禁止选择！',
            success: function(res) {

            }
          })
        }
      },  
      fail:function(res){  
          console.log('选课失败');  
      } 
    })
  },
  /*
    添加课程之后，使按钮变为不可选择的状态
  */
  setButton:function(e){
    this.data.type[e.currentTarget.id] = 'default';
    this.data.disabled[e.currentTarget.id] = true;
    this.setData({
      type:this.data.type,
      disabled:this.data.disabled
    });    
  },
  /*
    退课
  */
  exitClass:function(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定退掉此门课程？',
      success: function(res) {
        if(res.confirm){
          console.log('用户点击确定');
          that.del(e);
        }else if(res.cancel){
          console.log('用户点击取消');
        }
      }
    }) 
  },
  del:function(e){
    var that = this,
      index = e.currentTarget.id;
      console.log(that.data.baomingArray);
    wx.request({  
      url: this.data.delUrl,  
      method:"get",
      data: {
        parent_id:1,
        course_id:that.data.baomingArray[index].id
      }, 
      header: {'content-type': 'application/x-www-form-urlencoded'},  
      success:function(res) {
        if (res.data.code == 0){
          console.log('退课成功');
          that.setDelButton(e);
        }
      },  
      fail:function(res){  
          console.log('退课失败');  
      } 
    })
  },
  /*
    设置删除按钮的颜色
  */
  setDelButton:function(e){
    this.data.type1[e.currentTarget.id] = 'default';
    this.data.disabled1[e.currentTarget.id] = true;
    this.setData({
      type1:this.data.type1,
      disabled1:this.data.disabled1
    }); 
  }
})
